package com.ysd.lis.service.common.impl;

import com.ysd.lis.entity.SysParamSearchInputDto;
import com.ysd.lis.entity.SysParamSearchOutputDto;
import com.ysd.lis.service.SysParamValueService;
import com.ysd.lis.service.common.RedisSysParamHelper;
import com.ysd.util.ToolsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class RedisSysParamHelperImpl implements RedisSysParamHelper {

    @Autowired
    SysParamValueService sysParamValueService;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    // 使用 SCAN 命令检查键前缀是否存在
    public boolean checkPrefixKeyExistence(String prefix) {
        ScanOptions options = ScanOptions.scanOptions().match(prefix + ":*").build();

        // 使用 Java 8 风格，返回 Optional 包装的 cursor.hasNext()
        Optional<Boolean> result = Optional.ofNullable(redisTemplate.executeWithStickyConnection(conn -> conn.scan(options))).map(cursor -> cursor.hasNext()); // 使用 map 映射 Cursor 为 boolean

        // 使用 orElse 返回默认值
        return result.orElse(false); // 如果 result 为 null 或没有匹配项，则返回 false
    }


    public SysParamSearchOutputDto getValueByKeys(SysParamSearchInputDto param) {
        SysParamSearchOutputDto res = new SysParamSearchOutputDto();
        if (ToolsUtils.isEmpty(param.getOrgId())) {
            res.setStatusCode(-1);
            return res;
        }
        String key = "system:options:" + param.getOrgId();
        boolean exist = checkPrefixKeyExistence(key);
        if (!exist) {
            sysParamValueService.refreshRedis(param.getOrgId());
        }
        if (ToolsUtils.isNotEmpty(param.getParamCode())) {
            key += ":" + param.getParamCode();
        }
        if (ToolsUtils.isNotEmpty(param.getField())) {
            key += "." + param.getField();
        }
        if (ToolsUtils.isNotEmpty(param.getClassifyCode())) {
            key += "." + param.getClassifyCode();
        }

        if (!redisTemplate.hasKey(key)) {
            res.setStatusCode(1);
            return res;
        }
        String value = (String) redisTemplate.opsForValue().get(key);
        res.setStatusCode(2);
        res.setValueStr(value);
        return res;
    }

    @Override
    public SysParamSearchOutputDto getValueByXTXXXParam(String orgId, String key, String defaultStr) {
        SysParamSearchInputDto param = new SysParamSearchInputDto();
        param.setOrgId(orgId);
        param.setOrgId(ToolsUtils.isEmpty(orgId) ? "!@#QWE" : orgId);
        param.setParamCode("xtxxcs");
        param.setField(ToolsUtils.isEmpty(key) ? "!@#QWE" : key);
        SysParamSearchOutputDto paramResult = getValueByKeys(param);
        if (ToolsUtils.isEmpty(paramResult)) {
            paramResult = new SysParamSearchOutputDto();
            paramResult.setStatusCode(2);
            paramResult.setValueStr(ToolsUtils.isNotEmpty(defaultStr) ? defaultStr : null);/*危急值颜色默认显示为背景颜色*/
        }
        return paramResult;
    }

    @Override
    public SysParamSearchOutputDto getValueByBGDYXTXXCSParam(String orgId, String key, String rptGroup, String defaultStr) {
        SysParamSearchInputDto param = new SysParamSearchInputDto();
        param.setOrgId(ToolsUtils.isEmpty(orgId) ? "!@#QWE" : orgId);
        param.setParamCode("bgdyxtcs");
        param.setField(ToolsUtils.isEmpty(key) ? "!@#QWE" : key);
        param.setClassifyCode(ToolsUtils.isEmpty(rptGroup) ? "!@#QWE" : rptGroup);
        SysParamSearchOutputDto paramResult = getValueByKeys(param);
        if (ToolsUtils.isEmpty(paramResult)) {
            paramResult = new SysParamSearchOutputDto();
            paramResult.setStatusCode(2);
            paramResult.setValueStr(ToolsUtils.isNotEmpty(defaultStr) ? defaultStr : null);/*危急值颜色默认显示为背景颜色*/
        }
        return paramResult;
    }

    @Override
    public SysParamSearchOutputDto getValueByYQXTXXXParam(String orgId, String key, String instrCode, String defaultStr) {
        SysParamSearchInputDto param = new SysParamSearchInputDto();
        param.setOrgId(ToolsUtils.isEmpty(orgId) ? "!@#QWE" : orgId);
        param.setParamCode("yqxtxx");
        param.setField(ToolsUtils.isEmpty(key) ? "!@#QWE" : key);
        param.setClassifyCode(ToolsUtils.isEmpty(instrCode) ? "!@#QWE" : instrCode);
        SysParamSearchOutputDto paramResult = getValueByKeys(param);
        if (ToolsUtils.isEmpty(paramResult)) {
            paramResult = new SysParamSearchOutputDto();
            paramResult.setStatusCode(2);
            paramResult.setValueStr(ToolsUtils.isNotEmpty(defaultStr) ? defaultStr : null);/*危急值颜色默认显示为背景颜色*/
        }
        return paramResult;
    }

    @Override
    public SysParamSearchOutputDto getValueByLOCALXTXXXParam(String orgId, String key, String computerrName, String defaultStr) {
        SysParamSearchInputDto param = new SysParamSearchInputDto();
        param.setOrgId(ToolsUtils.isEmpty(orgId) ? "!@#QWE" : orgId);
        param.setParamCode("bdxtcs");
        param.setField(ToolsUtils.isEmpty(key) ? "!@#QWE" : key);
        param.setClassifyCode(ToolsUtils.isEmpty(computerrName) ? "!@#QWE" : computerrName);
        SysParamSearchOutputDto paramResult = getValueByKeys(param);
        if (ToolsUtils.isEmpty(paramResult)) {
            paramResult = new SysParamSearchOutputDto();
            paramResult.setStatusCode(2);
            paramResult.setValueStr(ToolsUtils.isNotEmpty(defaultStr) ? defaultStr : null);/*危急值颜色默认显示为背景颜色*/
        }
        return paramResult;
    }
}
